package com.movies.service.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movies.service.common.Result;
import com.movies.service.entity.Cinema;
import com.movies.service.service.CinemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user/cinema")
public class UserCinemaController {

    @Autowired
    private CinemaService cinemaService;

//    分页查询影院列表（支持名称模糊、状态筛选）
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String cname,
                       @RequestParam(required = false) String status) {
        LambdaQueryWrapper<Cinema> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(cname != null && !cname.isEmpty(), Cinema::getCname, cname)
                .eq(status != null && !status.isEmpty(), Cinema::getStatus, status);
        Page<Cinema> page = cinemaService.page(new Page<>(pageNo, pageSize), wrapper);
        return Result.success(page);
    }

//    关键字搜索影院（按名称或地址）
    @GetMapping("/search")
    public Result search(@RequestParam String keyword,
                         @RequestParam(defaultValue = "1") Integer pageNo,
                         @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Cinema> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Cinema::getCname, keyword).or().like(Cinema::getAddress, keyword);
        Page<Cinema> page = cinemaService.page(new Page<>(pageNo, pageSize), wrapper);
        return Result.success(page);
    }

//    获取影院详情
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        return Result.success(cinemaService.getById(id));
    }
}